﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ModbusCollect.entity;
using ModbusCollect.Services;

namespace ModbusCollect.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ModbusController : ControllerBase
    {
        private readonly ModbusTcpService _modbusService;
        public ModbusController(ModbusTcpService modbusService)
        {
            _modbusService = modbusService;
        }

        /// <summary>
        /// 读取保持寄存器
        /// </summary>
        /// <param name="address">起始寄存器地址</param>
        /// <param name="count">读取数据个数</param>
        /// <param name="slaveId">从站ID，默认1</param>
        /// <param name="dataType">数据类型，支持：int16, uint16, int32, uint32, float, double, long, ulong</param>
        [HttpGet("read")]
        public IActionResult ReadHoldingRegisters(
            [FromQuery] string address,
            [FromQuery] byte code,
            [FromQuery] byte slaveId = 1,
            [FromQuery] string dataType = "int16")
        {
            try
            {
                switch (dataType.ToLower())
                {
                    case "int16":
                        return Ok(_modbusService.ReadHoldingRegisters<short>(address, slaveId, code));
                    case "uint16":
                        return Ok(_modbusService.ReadHoldingRegisters<ushort>(address, slaveId, code));
                    case "int32":
                        return Ok(_modbusService.ReadHoldingRegisters<int>(address, slaveId, code));
                    case "uint32":
                        return Ok(_modbusService.ReadHoldingRegisters<uint>(address, slaveId, code));
                    case "float":
                        return Ok(_modbusService.ReadHoldingRegisters<float>(address, slaveId, code));
                    case "double":
                        return Ok(_modbusService.ReadHoldingRegisters<double>(address, slaveId, code));
                    case "long":
                        return Ok(_modbusService.ReadHoldingRegisters<long>(address, slaveId, code));
                    case "ulong":
                        return Ok(_modbusService.ReadHoldingRegisters<ulong>(address, slaveId, code));
                    default:
                        return BadRequest("不支持的数据类型");
                }
            }
            catch (Exception ex)
            {
                return BadRequest($"读取失败: {ex.Message}");
            }
        }


        [HttpPost("write")]
        public IActionResult WriteRegister([FromBody] WriteRequest request)
        {
            try
            {
                switch (request.DataType.ToLower())
                {
                    case "int16":
                        _modbusService.WriteRegister<short>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "uint16":
                        _modbusService.WriteRegister<ushort>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "int32":
                        _modbusService.WriteRegister<int>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "uint32":
                        _modbusService.WriteRegister<uint>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "long":
                        _modbusService.WriteRegister<long>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "ulong":
                        _modbusService.WriteRegister<ulong>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "float":
                        _modbusService.WriteRegister<float>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    case "double":
                        _modbusService.WriteRegister<double>(request.Address, request.Value, request.SlaveId, request.FunctionCode);
                        break;
                    default:
                        return BadRequest("不支持的数据类型");
                }
                return Ok("写入成功");
            }
            catch (Exception ex)
            {
                return BadRequest($"写入失败: {ex.Message}");
            }
        }



    }
}
